﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongMau;

namespace QuanLyKho.Control
{
    public partial class Phongmau_DkSanpham : UserControl
    {
        public Phongmau_DkSanpham()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            var tuNgay = dtTuNgay.Checked ? dtTuNgay.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/01/" + DateTime.Now.Year;
            var denNgay = dtDenNgay.Checked ? dtDenNgay.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/"
                + DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) + "/" + DateTime.Now.Year;
            var table = DataAll.SelectAll("PhongMau_SanPham", "*", "WHERE (TrangThai = " + Convert.ToInt32(TrangThai.PhongMau_TaoMoi)
                + " OR TrangThai = " + Convert.ToInt32(TrangThai.PhongMau_KhongDuyet) + ") AND (CONVERT(DATE, NgayThang, 0) >= CONVERT(DATE,'"
                + tuNgay + "', 0) AND CONVERT(DATE, NgayThang, 0) <= CONVERT(DATE,'" + denNgay + "' , 0))");
            var imconverter = new ImageConverter();
            var columns = new DataColumn("GuiDuyet", typeof(byte[]));
            columns.DefaultValue = imconverter.ConvertTo(Properties.Resources.export_gray, typeof(byte[]));
            table.Columns.Add(columns);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (Convert.ToInt32(table.Rows[i]["TrangThai"]) == Convert.ToInt32(TrangThai.PhongMau_TaoMoi)
                    || Convert.ToInt32(table.Rows[i]["TrangThai"]) == Convert.ToInt32(TrangThai.PhongMau_KhongDuyet))
                {
                    table.Rows[i]["GuiDuyet"] = imconverter.ConvertTo(Properties.Resources.export, typeof(byte[]));
                }
            }
            gridSanPham.DataSource = table;
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvSanPham.BestFitColumns();
            gvSanPham.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvSanPham.OptionsBehavior.Editable = false;
            gvSanPham.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvSanPham.OptionsSelection.MultiSelect = true;
            gvSanPham.OptionsView.ColumnAutoWidth = false;
            gvSanPham.OptionsView.ShowFooter = true;
            gvSanPham.Columns["NguoiDangKy"].Fixed = FixedStyle.Left;
            for (int i = 2; i < gvSanPham.Columns.Count; i++)
            {
                gvSanPham.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        private void bntThemMoi_Click(object sender, EventArgs e)
        {
            Phongmau_Themsanpham.xem = true;
            var themsanpham = new Phongmau_Themsanpham();
            themsanpham.ShowDialog();
            LoadData();
            Phongmau_Themsanpham.xem = false;
        }

        private void gvNhapkho_DoubleClick(object sender, EventArgs e)
        {
            if (gvSanPham.FocusedRowHandle >= 0)
            {
                Phongmau_Themsanpham.MaID = gvSanPham.GetDataRow(gvSanPham.FocusedRowHandle)["IdSanPham"].ToString();
                Phongmau_Themsanpham.xem = true;
                var themsp = new Phongmau_Themsanpham();
                themsp.update = true;
                themsp.ShowDialog();
                LoadData();
                Phongmau_Themsanpham.MaID = "0";
                Phongmau_Themsanpham.xem = false;
            }
        }

        private void gvSanPham_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void btDuyetSP_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvSanPham.DataSource;
            string a = gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.PhongMau_TaoMoi).ToString();
            string c = Convert.ToInt32(TrangThai.PhongMau_KhongDuyet).ToString();
            if (a == b || a == c)
            {

                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi duyệt sản phẩm.", "Message",
                                                            MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("PhongMau_SanPham",
                                                    "TrangThai =" + Convert.ToInt32(TrangThai.PhongMau_ChoDuyet) +
                                                    ",NgayGui ='" + DateTime.Now.ToString("MM/dd/yyyy") +
                                                    "' WHERE IdSanPham =" +
                                                    dataview[gvSanPham.FocusedRowHandle]["IdSanPham"]);

                    MessageBox.Show(message.Equals("success") ? "Gửi duyệt sản phẩm thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    LoadData();
                }
            }
        }

        private void peDuyet_Click(object sender, EventArgs e)
        {
            var dataview = (DataView)gvSanPham.DataSource;
            string a = gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.PhongMau_TaoMoi).ToString();
            string c = Convert.ToInt32(TrangThai.PhongMau_KhongDuyet).ToString();
            if (a == b || a == c)
            {

                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi duyệt sản phẩm.", "Message",
                                                            MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("PhongMau_SanPham",
                                                    "TrangThai =" + Convert.ToInt32(TrangThai.PhongMau_ChoDuyet) +
                                                    ",NgayGui ='" + DateTime.Now.ToString("MM/dd/yyyy") +
                                                    "' WHERE IdSanPham =" +
                                                    dataview[gvSanPham.FocusedRowHandle]["IdSanPham"]);

                    MessageBox.Show(message.Equals("success") ? "Gửi duyệt sản phẩm thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    LoadData();
                }
            }
        }

        private void gvSanPham_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "DenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 <= songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }

        private void dtTuNgay_Leave(object sender, EventArgs e)
        {
            LoadData();
        }

        private void dtDenNgay_Leave(object sender, EventArgs e)
        {
            LoadData();
        }

        private void gvSanPham_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                GridView View = sender as GridView;
                string trangthai = View.GetRowCellDisplayText(e.RowHandle, View.Columns["TrangThai"]);
                if (trangthai == "Chờ duyệt")
                {
                    e.Appearance.BackColor = Color.SandyBrown;
                }
                else if (trangthai == "Không duyệt")
                {
                    e.Appearance.BackColor = Color.Salmon;
                }
                else
                {
                    e.Appearance.BackColor = Color.Empty;
                }

            }
        }

        private void btnXoa_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvSanPham.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa sản phẩm ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
               var message=  DataAll.Delete("PhongMau_SanPham", "IdSanPham = " + dataview[gvSanPham.FocusedRowHandle]["IdSanPham"]);
                MessageBox.Show(message.Equals("success") ? "Xóa sản phẩm thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
            
        }
    }
}
